package com.bhs.sansonglogistics.ui.waybill;

import static com.google.android.material.tabs.TabLayout.MODE_FIXED;

import androidx.annotation.NonNull;
import androidx.constraintlayout.motion.widget.FloatLayout;
import androidx.viewpager2.widget.ViewPager2;

import android.app.DatePickerDialog;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.LineDataBean;
import com.bhs.sansonglogistics.bean.LineListBean;
import com.bhs.sansonglogistics.bean.TabBean;
import com.bhs.sansonglogistics.bean.event.Event;
import com.bhs.sansonglogistics.bean.event.ScreeningEvent;
import com.bhs.sansonglogistics.dialog.TopScreenDialog;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.home.FragmentAdapter;
import com.bhs.sansonglogistics.ui.order.UpstreamShipmentOrderFragment;
import com.bhs.sansonglogistics.view.SelectedFont;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.interfaces.SimpleCallback;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 上游来货
 */
public class UpstreamShipmentActivity extends BaseActivity implements NetCallBack, View.OnClickListener {

    private TextView mTvTitle;
    private EditText mEtSearch;
    private TabLayout mTabLayout;
    private ViewPager2 mViewPage;
    private List<TabBean> tabBeanList = new ArrayList<>();
    private List<LineListBean> data;
    public FrameLayout mFlScreen;
    private TopScreenDialog dialog;


    @Override
    public int initLayout() {
        return R.layout.activity_upstream_shipment;
    }

    @Override
    public void initView() {
        mTvTitle = findViewById(R.id.tv_title);
        mEtSearch = findViewById(R.id.et_search);
        mTabLayout = findViewById(R.id.tabLayout);
        mViewPage = findViewById(R.id.view_page);
        mTvTitle.setText("上游来货");
        findViewById(R.id.tv_screen).setOnClickListener(this);
        networkRequest(netApi.getUpWaybillLineList("0"), this);
        mFlScreen = findViewById(R.id.fl_screen);
        dialog = new TopScreenDialog(this);
        mFlScreen.setOnClickListener(this);
        findViewById(R.id.ll_screen).setOnClickListener(this);
        mEtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //点击搜索要做的操作
                    EventBus.getDefault().post(new ScreeningEvent(mEtSearch.getText().toString()));
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void initData() {
        tabBeanList.add(new TabBean("待接收", "0"));
        tabBeanList.add(new TabBean("已到达", "1"));
        tabBeanList.add(new TabBean("已入库", ""));
        mTabLayout.setTabMode(MODE_FIXED);
        //tab切换时候的字体设置
        mTabLayout.addOnTabSelectedListener(new SelectedFont(this));
        ArrayList<BaseFragment> fragments = new ArrayList<>();

        fragments.add(UpstreamShipmentListFragment.newInstance(tabBeanList.get(0).getType()));
        fragments.add(UpstreamShipmentListFragment.newInstance(tabBeanList.get(1).getType()));
        fragments.add(UpstreamShipmentOrderFragment.newInstance());
        mViewPage.setOffscreenPageLimit(fragments.size());
        mViewPage.setAdapter(new FragmentAdapter(this, fragments));
        new TabLayoutMediator(mTabLayout, mViewPage, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(tabBeanList.get(position).getTypeName());
            }
        }).attach();
    }


    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        LineDataBean lineDataBean = new Gson().fromJson(json, LineDataBean.class);
        if (lineDataBean.isStatus()) {
            data = lineDataBean.getData().getLine_list();
            dialog.setData(data);
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }

    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.fl_screen){
            mFlScreen.setVisibility(View.GONE);
        }else if(view.getId()==R.id.tv_screen){
            mFlScreen.setVisibility(View.VISIBLE);
        }
    }


}